package com.kuang.thread.state;

public class TestStop implements Runnable{
    private boolean runThread = true;

    public void run(){
        int i=0;
        while (runThread){
            System.out.println("run..."+i++);
        }
    }
    //对外部提供方法改变标识
    public void stop(){
        this.runThread = false;
    }

    public static void main(String[] args) {
        TestStop testStop = new TestStop();
        new Thread(testStop).start();

        for (int i = 0; i < 10000; i++) {
            System.out.println("main");
            if (i==9000){
                //调用stop方法切换标志位，让线程停止
                testStop.stop();
                System.out.println("线程停止了");
            }
        }
    }
}
